//
//  JSQShowImageViewController.m
//  Conversation
//
//  Created by C_HAO on 15/10/16.
//  Copyright © 2015年 CHAOO. All rights reserved.
//

#import "JSQShowImageViewController.h"
#import "JSQShowImageScrollView.h"
#import "JSQImageMarkLabel.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface JSQShowImageViewController () <ShowImageScrollViewDelegate, ShowImageScrollViewDataSource, UIActionSheetDelegate>

//展示板
@property(nonatomic, strong) JSQShowImageScrollView *showImageScrollView;
//标记
@property(nonatomic, strong) JSQImageMarkLabel *marklabel;


@end

@implementation JSQShowImageViewController

- (instancetype)initWithSourceData:(NSArray<JSQMessage *> *)data withIndex:(NSInteger)index {
    self = [super init];
    if (self) {
        self = [self init];

        _data = [NSMutableArray arrayWithArray:data];
        _index = index + 1;

    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    self.automaticallyAdjustsScrollViewInsets = NO;

    self.view.backgroundColor = [UIColor blackColor];

    [self creatImageShow];

    _marklabel = [[JSQImageMarkLabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80)/2, self.view.frame.size.height - 44, 80, 22) withTotal:_data.count];

    [_marklabel showPageNumber:_index];

    [self.view addSubview:_marklabel];

    if (self.delegate) {
        //右下脚按钮
        UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        saveButton.center = CGPointMake(self.view.frame.size.width-22, _marklabel.center.y);
        saveButton.backgroundColor = [UIColor clearColor];
        [saveButton setBackgroundImage:[UIImage jsq_imageOperation] forState:UIControlStateNormal];
        [saveButton addTarget:self action:@selector(saveAndCollection) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:saveButton];
    }
}

// 初始化视图
- (void)creatImageShow {
    _showImageScrollView = [[JSQShowImageScrollView alloc] initWithFrame:self.view.bounds withIndex:_index - 1];
    _showImageScrollView.imageDelegate = self;
    _showImageScrollView.imageDataSource = self;

    _showImageScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    __weak typeof(self) weakSelf = self;

    _showImageScrollView.slideScrollPage = ^(NSInteger nmber) {

        weakSelf.index = nmber;
        [weakSelf.marklabel showPageNumber:nmber];
    };

    [self.view addSubview:_showImageScrollView];

    [_showImageScrollView reloadData];
}

//对应的ShowZoomImageScrollView
- (JSQShowZoomImageScrollView *)scrollView:(JSQShowImageScrollView *)scrollView viewForItemAtIndex:(NSInteger)index {
    static NSString *identifier = @"ShowZoomImageScrollView";
    JSQShowZoomImageScrollView *view = (JSQShowZoomImageScrollView *)[scrollView dequeueReusableViewWithIdentifier:identifier];
    if (view == nil) {
        view = [[JSQShowZoomImageScrollView alloc] initWithImageReuseIdentifier:identifier];
        view.parent = self;
    }

    JSQMessage *message = [_data objectAtIndex:index];

    if (![[message mediaData] isKindOfClass:[JSQPhotoMediaItem class]]) {
        return nil;
    }

    JSQPhotoMediaItem *photoMediaItem = (JSQPhotoMediaItem *)[message mediaData];

    view.imageView.image = photoMediaItem.image;
    _showImageView = view;

    return view;
}

//数量
- (NSInteger)numberOfItemsInScrollView:(JSQShowImageScrollView *)scrollView {
    return _data.count;
}

- (UIEdgeInsets)scrollView:(JSQShowImageScrollView *)scrollView insetForForItemAtIndex:(NSInteger)index {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark -UIGestureReconginzer
- (void)tapAction:(UITapGestureRecognizer *)tap {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark Touch Button Action


- (void)saveAndCollection {
    JSQMessage *message = [_data objectAtIndex:_index - 1];
    if (![[message mediaData] isKindOfClass:[JSQPhotoMediaItem class]]) {
        return;
    }
    JSQPhotoMediaItem *photoMediaItem = (JSQPhotoMediaItem *)[message mediaData];
    UIImage *imageForSave = photoMediaItem.image;

    [self.delegate showImageViewMoreOperation:self showImage:imageForSave];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
